//
//  GJFMapViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/7.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFMapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MKMapView+ZoomLevel.h"

@interface GJFMapViewController () <MKMapViewDelegate, CLLocationManagerDelegate>
{
    MKMapView *_mapView;
    MKPointAnnotation *myAnnotation;
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}
@end

@implementation GJFMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"地图";
    
    _mapView =[[MKMapView alloc] initWithFrame:self.view.bounds];
    _mapView.zoomEnabled = YES;//缩放
    _mapView.scrollEnabled = YES;
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-返回"] style:UIBarButtonItemStyleDone target:self action:@selector(popVc)];
}

- (void)popVc {
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)goLocationWithLatitude:(NSString *)latitude Longitude:(NSString *)longitude {
    [self reocoderMethodLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
}

#pragma mark - 逆地理转化
- (void)reocoderMethodLatitude:(CGFloat)latitude longitude:(CGFloat)longitude {
    [self.view showBusyHUD];
    CLLocation *location0 = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    CLGeocoder *reocoder = [[CLGeocoder alloc] init];
    [reocoder reverseGeocodeLocation:location0 completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        [self.view hideBusyHUD];
        if (error) {
            NSLog(@"逆地理转化失败:%@", error);
        }
        else if (placemarks.count == 0) {
            NSLog(@"没有返回值!!!");
        }
        else {
            CLPlacemark *placemark = placemarks.firstObject;

            CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(latitude, longitude);
            myAnnotation = [[MKPointAnnotation alloc] init];
            myAnnotation.title = placemark.locality;
            myAnnotation.subtitle = [placemark.addressDictionary[@"FormattedAddressLines"] firstObject];
            myAnnotation.coordinate = location1;
            [_mapView addAnnotation:myAnnotation];
            [_mapView setCenterCoordinate:location1 zoomLevel:14 animated:YES];
            [self performSelector:@selector(showAnnotationsDetailInfo) withObject:self afterDelay:2];
        }
    }];
}
//显示大头针对应的位置
- (void)showAnnotationsDetailInfo {
    [_mapView selectAnnotation:myAnnotation animated:YES];
}


// 每次添加大头针都会调用此方法  可以设置大头针的样式
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    // 判断大头针位置是否在原点,如果是则不加大头针
    if([annotation isKindOfClass:[mapView.userLocation class]]) {
        return nil;
    }
    static NSString *annotationName = @"annotation";
    
    MKPinAnnotationView *anView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationName];
    
    if(anView == nil)
    {
        anView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationName];
    }
    anView.animatesDrop = YES;
    //    // 显示详细信息
    anView.canShowCallout = YES;
    //    anView.leftCalloutAccessoryView   可以设置左视图
    //    anView.rightCalloutAccessoryView   可以设置右视图
    return anView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
